Tips&Tricks I trucchi del mestiere

 

Come richiamare la finestra "Trova" del men∙ Start

Il tips proposto, consente di richiamare la funzione "Trova" di Windows. Come Φ facile intuire dal codice, il tips utilizza l'API di sistema ShellExecute. Tale API, usata opportunamente, consente di eseguire molte operazioni prettamente di sistema.

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
           (ByVal hwnd As Long, ByVal lbOperation As String, ByVal lpFile As String, _
            ByVal lpParameters As String, ByVal lpDirectory As String, _
            ByVal nShowCmd as Long)

Const SW_SHOW = 5

Private Sub CmdTrova_Click()
            ShellExecute Me.hWnd, "find", "c:\", vbNullString, SW_SHOW
End Sub

Come ottenere info sulla Barra delle Applicazioni di Windows

In alcune applicazioni potrebbe rendersi necessario verificare alcune impostazioni della barra delle applicazioni di Windows; per esempio si vorrebbe poter verificare se le opzioni di "Nascondi automaticamente" e/o "Sempre in primo piano" sono attive o meno.


Const ABS_AUTOHIDE = &H1

Const ABS_ONTOP = &H2

Const ABM_GETSTATE = &H4

Const ABM_GETTASKBARPOS = &H5

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Type APPBARDATA
    cbSize As Long
    hwnd As Long
    uCallbackMessage As Long
    uEdge As Long
    rc As RECT
    lParam As Long 
End Type

Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData 
As APPBARDATA) As Long

Private Sub Form_Paint()
    Dim ABD As APPBARDATA, Ret As Long
    SHAppBarMessage ABM_GETTASKBARPOS, ABD
    Ret = SHAppBarMessage(ABM_GETSTATE, ABD)
    If (Ret And ABS_AUTOHIDE) Then Me.Print "L'opzione NASCONDI AUTOMATICAMENTE Φ 
attiva"

    If (Ret And ABS_ONTOP) Then Me.Print "l'opzione SEMPRE IN PRIMO PIANO Φ attiva"
    Me.Print "Le coordinate della barra delle applicazioni: (" + Trim(Str(ABD.rc.Left)) + "," + 
Trim(Str(ABD.rc.Top)) + ")-(" + Trim(Str(ABD.rc.Right)) + "," + 
Trim(Str(ABD.rc.Bottom)) + ")"
End Sub

Come inviare una stringa di testo ad una shell DOS

Il tips proposto consente di utilizzare la clipboard per "immagazzinare" e successivamente inviare, una stringa di testo alla shell DOS. In questo esempio, viene simulato il passaggio della stringa "dir *.*" e la pressione del tasto invio; ricordiamo, se ce ne fosse bisogno, che la stringa "dir *.*" permette di reperire l'elenco di tutti i file presenti nella directory corrente.

Private Sub Command1_Click()
    Shell ("command.com"), vbMaximizedFocus
End Sub

Private Sub Command2_Click()
    Clipboard.Clear
    Clipboard.SetText "Dir *.*" + Chr$(13)
    AppActivate "Prompt di MS-DOS"
    SendKeys "% ep", 1
End Sub

Come richiamare la finestra delle proprietα di un file

Spesso Φ fondamentale sapere la data di creazione di un particolare file, lo spazio occupato su disco e diverse altre proprietα. Il tips consente di ricavare tutte le informazioni relative alle proprietα di un file, cos∞ come normalmente accade cliccando con il tasto destro del mouse e richiamando la voce Proprietα.

Option Explicit

  Private Type SHELLEXECUTEINFO
      cbSize        As Long
      fMask         As Long
      hWnd          As Long
      lpVerb        As String
      lpFile        As String
      lpParameters  As String
      lpDirectory   As String
      nShow         As Long
      hInstApp      As Long
      lpIDList      As Long     'Optional; ignore
      lpClass       As String   'Optional; ignore
      hkeyClass     As Long     'Optional; ignore
      dwHotKey      As Long     'Optional; ignore
      hIcon         As Long     'Optional; ignore
      hProcess      As Long     'Optional; ignore
  End Type

  Private Const SEE_MASK_INVOKEIDLIST = &HC

  Private Const SEE_MASK_NOCLOSEPROCESS = &H40

  Private Const SEE_MASK_FLAG_NO_UI = &H400


  Private Declare Function ShellExecuteEx Lib "shell32" Alias "ShellExecuteExA" (sei As 
SHELLEXECUTEINFO) As Long

  Private Sub ShowProperties(strFilename As String, hWnd As Long)
      Dim lngReturn As Long
      Dim sei As SHELLEXECUTEINFO

      With sei
          .cbSize = Len(sei)
          .fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_INVOKEIDLIST Or 
SEE_MASK_FLAG_NO_UI
          .hWnd = hWnd
          .lpVerb = "properties"
          .lpFile = strFilename
          .lpParameters = vbNullChar
          .lpDirectory = vbNullChar
          .nShow = 0
          .hInstApp = 0
          .lpIDList = 0
      End With
      lngReturn = ShellExecuteEx(sei)
  End Sub

Private Sub Command1_Click()
      ShowProperties "c:\winnt\explorer.exe", Me.hWnd

End Sub
	  

Come invocare un report Access da Visual Basic

E' risaputo che Microsoft Access offre un sistema di report molto pi∙ all'avanguardia rispetto a Visual Basic 6.0. Proprio per questo motivo, il codice che segue pu≥ essere molto utile se si decide di avviare una stampa di un report Access direttamente da un'applicazione VB.

Dim objAccess As Object

Private Sub Command1_Click()
Dim dbName As String
Dim rptName As String
Dim Preview As Long
Const acNormal = 0
Const acPreview = 2

dbName = "prova.mdb"
rptName = "Nome_Report"
Preview = acPreview 'acNormal

With objAccess
    .OpenCurrentDatabase filepath:=dbName
    If Preview = acPreview Then
       .Visible = True
       .DoCmd.OpenReport rptName, Preview
    Else
       .DoCmd.OpenReport rptName
    End If
End With
End Sub

Private Sub Form_Load()
Set objAccess = CreateObject("Access.Application")
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
objAccess.Quit
On Error GoTo 0
Set objAccess = Nothing
End Sub